package com.mypro.guava;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Joiner;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * @author 玄夏
 * @date 2018/6/20 23:46
 */
public class OptionalDemo {
    public static void main(String[] args){
        System.out.println(Joiner.on("#").skipNulls().join(Arrays.asList(1,2,3,4,null,6)));

        EventBus eventBus=new EventBus("jack");
       /*
         如果多个subscriber订阅了同一个事件,那么每个subscriber都将收到事件通知
         并且收到事件通知的顺序跟注册的顺序保持一致
        */
        eventBus.register(new EventListener()); //注册订阅者
        eventBus.register(new MultiEventListener());
        eventBus.post(new OrderEvent("hello")); //发布事件
        eventBus.post(new OrderEvent("world"));
        eventBus.post("!");

        Cache<String,String> cache= CacheBuilder.newBuilder()
            .maximumSize(100)
            .expireAfterAccess(1, TimeUnit.SECONDS)
            .build();


    }

    private static class OrderEvent{
        private String message;

        public OrderEvent(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }

    private static class EventListener{
        //@Subscribe保证有且只有一个输入参数,如果你需要订阅某种类型的消息,只需要在指定的方法上加上@Subscribe注解即可
        @Subscribe
        public void listen(OrderEvent event){
            System.out.println("EventListener receive message: "+event.getMessage());
        }

        /*
          一个subscriber也可以同时订阅多个事件
          Guava会通过事件类型来和订阅方法的形参来决定到底调用subscriber的哪个订阅方法
         */
        @Subscribe
        public void listen(String message){
            System.out.println("EventListener receive message: "+message);
        }
    }

    private static class MultiEventListener{
        @Subscribe
        public void listen(OrderEvent event){
            System.out.println("MultiEventListener receive msg: "+event.getMessage());
        }

        @Subscribe
        public void listen(String message){
            System.out.println("MultiEventListener receive msg: "+message);
        }
    }
}
